//
//  KindFoodViewController.m
//  MeiShiDa
//
//  Created by duanshiyi on 15/7/3.
//  Copyright (c) 2015年 zby. All rights reserved.
//
#define SCREEN_WIDTH    [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT   [[UIScreen mainScreen] bounds].size.height
#import "KindFoodViewController.h"
#import "KindsCollectionViewCell.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>
@interface KindFoodViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation KindFoodViewController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        self.dataArray=[NSMutableArray array];
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationItem.title=@"美味菜单";
    [self createNetworking];
    [self createView];
}

//请求数据
-(void)createNetworking{
    NSString *urlStr =@"http://www.tudali.com:7177/App/CaiDan/FenLei";
    NSString *otherStr=@"ContentId=5f6d2fec0a1b3aa6";
    
    //    NSString *URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //转码成UTF-8  否则可能会出现错误
//    NSString *otherStr1=[otherStr stringByReplacingOccurrencesOfString:@"Start=0" withString:[NSString stringWithFormat:@"Start=%ld",(long)_start]];
    
    NSString *url=[NSString stringWithFormat:@"%@?%@",urlStr,otherStr];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *requestTmp = [NSString stringWithString:operation.responseString];
        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
        //系统自带JSON解析
        NSArray *arr= [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
        self.dataArray=[NSMutableArray arrayWithArray:arr];
       
        [_collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", error);
        [SVProgressHUD dismiss];
    }];
    [operation start];
}







-(void)createView{
    UIButton  *button1=[UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame=CGRectMake(0, 65 , SCREEN_WIDTH/2, 44);
//    button1.backgroundColor=[UIColor cyanColor];
    [button1 setTitle:@"人气菜品" forState:UIControlStateNormal];
    [self.view addSubview:button1];
    [button1 addTarget:self action:@selector(goMemu:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton  *button2=[UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame=CGRectMake(SCREEN_WIDTH/2, 65 , SCREEN_WIDTH/2, 44);
//    button2.backgroundColor=[UIColor cyanColor];
    [button2 setTitle:@"新品推荐" forState:UIControlStateNormal];
    [self.view addSubview:button2];
    [button2 addTarget:self action:@selector(goMemu:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 109, SCREEN_WIDTH, 1)];
    view.backgroundColor=[UIColor grayColor];
    [self.view addSubview:view];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(100,30);//给我们每个item的大小进行赋值
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 110, SCREEN_WIDTH-20, SCREEN_HEIGHT-110) collectionViewLayout:layout];
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.dataSource = self;//数据源协议
    _collectionView.delegate = self;//代理协议
    
    //注册一个UICollectionViewCell的类
    [_collectionView registerClass:[KindsCollectionViewCell class] forCellWithReuseIdentifier:@"abc"];
    
    [self.view addSubview:_collectionView];
 
    
}

-(void)goMemu:(UIButton *)sender{
    [_titleDelegate kindFoodVC:self navigationTitle:sender.titleLabel.text];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

//collectionView  代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [_dataArray count];
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KindsCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"abc" forIndexPath:indexPath];
    if ([_dataArray count]>1) {
        cell.label.text=_dataArray[indexPath.row][@"Name"];
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [_titleDelegate kindFoodVC:self navigationTitle:_dataArray[indexPath.row][@"Name"] ];
//    [self changeLayout];
    [self.navigationController popViewControllerAnimated:YES];
    
}


//改变布局加动画
- (void)changeLayout{
    NSLog(@"gaibian1");
    CATransition *animation=[CATransition animation];
    animation.type=@"rippleEffect";
    animation.subtype=kCATransitionFromLeft;
    animation.duration=1.5;
    [self.view.superview.layer addAnimation:animation forKey:@"donghua"];
    [self.navigationController popToRootViewControllerAnimated:YES];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
